<?php
/**
 * +--------------------------------------------------------------
 * | eLuoPHP
 * +--------------------------------------------------------------
 * | Copyright (c) 2016-2019 http://qxwoo.cn All rights reserved.
 * +--------------------------------------------------------------
 * | y139w [y139w@163.com]
 * +--------------------------------------------------------------
 * | 2019/7/11 9:57
 * +--------------------------------------------------------------
 * | Bank.php
 * +--------------------------------------------------------------
 */

namespace luo\traits;


trait Bank
{

    public static function test($bankno)
    {

        //取出最后一位（与luhm进行比较）
        $lastNum = substr($bankno, strlen($bankno) - 1, 1);
        //前15或18位
        $first15Num = substr($bankno, 0, strlen($bankno) - 1);
        $newArr = [];
        //前15或18位倒序存进数组
        for ($i = strlen($first15Num) - 1; $i > -1; $i--) {
            array_push($newArr, substr($first15Num, $i, 1));
        }
        //奇数位*2的积 <9
        $arrJiShu = [];
        //奇数位*2的积 >9
        $arrJiShu2 = [];
        //偶数位数组
        $arrOuShu = [];
        for ($j = 0; $j < count($newArr); $j++) {
            if (($j + 1) % 2 == 1) {
                //奇数位
                if ((int)$newArr[$j] * 2 < 9) {
                    array_push($arrJiShu, $newArr[$j] * 2);
                } else {
                    array_push($arrJiShu2, $newArr[$j] * 2);
                }
            } else {
                //偶数位
                array_push($arrOuShu, $newArr[$j]);
            }
        }
        //奇数位*2 >9 的分割之后的数组个位数
        $jishu_child1 = [];
        //奇数位*2 >9 的分割之后的数组十位数
        $jishu_child2 = [];
        for ($h = 0; $h < count($arrJiShu2); $h++) {
            array_push($jishu_child1, (int)$arrJiShu2[$h] % 10);
            array_push($jishu_child2, (int)$arrJiShu2[$h] / 10);
        }
        //奇数位*2 < 9 的数组之和
        $sumJiShu = 0;
        //偶数位数组之和
        $sumOuShu = 0;
        //奇数位*2 >9 的分割之后的数组个位数之和
        $sumJiShuChild1 = 0;
        //奇数位*2 >9 的分割之后的数组十位数之和
        $sumJiShuChild2 = 0;
        $sumTotal = 0;
        for ($m = 0; $m < count($arrJiShu); $m++) {
            $sumJiShu = $sumJiShu + (int)$arrJiShu[$m];
        }
        for ($n = 0; $n < count($arrOuShu); $n++) {
            $sumOuShu = $sumOuShu + (int)($arrOuShu[$n]);
        }
        for ($p = 0; $p < count($jishu_child1); $p++) {
            $sumJiShuChild1 = $sumJiShuChild1 + (int)($jishu_child1[$p]);
            $sumJiShuChild2 = $sumJiShuChild2 + (int)($jishu_child2[$p]);
        }
        //计算总和
        $sumTotal = (int)($sumJiShu) + (int)($sumOuShu) + (int)($sumJiShuChild1) + (int)($sumJiShuChild2);
        //计算Luhm值
        $k = (int)($sumTotal) % 10 == 0 ? 10 : (int)($sumTotal) % 10;
        $luhm = 10 - $k;
        if ($lastNum == $luhm) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 长度验证
     * @param $bankno
     * @return bool
     */
    public static function check($bankno){

        $bankLen = strlen($bankno);
        if($bankLen >= 16 && $bankLen <= 19){
            return true;
        }else{
            return false;
        }

    }


}